package m.jclecai.com.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.TabFragmentPagerAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.callback.GetMatchNameBack;
import m.jclecai.com.callback.ItemClickUtil;
import m.jclecai.com.dialog.BiFenChooseGameDialog;
import m.jclecai.com.fragment.bifen.BiFenChildFragment;
import m.jclecai.com.inter.IGetMatchsNames;
import m.jclecai.com.inter.OnResultListener;

import static m.jclecai.com.utils.DateUtil.getFormatTime;
import static m.jclecai.com.utils.DateUtil.getSystemCurrentMills;


/**
 * 购彩页面
 */
public class BiFenFragment extends BaseFragment implements View.OnClickListener, IGetMatchsNames {

    @BindView(R.id.tabitem_1)
    TextView tabitem_1;

    @BindView(R.id.tabitem_2)
    TextView tabitem_2;

    @BindView(R.id.tv_bifen_saishi_choose)
    ImageView tv_bifen_saishi_choose;
    @BindView(R.id.vp_bifen)
    ViewPager vp_bifen;

    private List<Fragment> list;
    private TabFragmentPagerAdapter adapter;

    private String[] matchNames;

    @BindView(R.id.tr_day)
    TableRow tr_day;
    @BindView(R.id.tr_week)
    TableRow tr_week;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_bifen;
    }

    @Override
    public void initView() {
        initTag(0);
        initWeekdays();//初始化日期
        initViewPager();
    }

    private void initWeekdays() {
        String[] days = new String[7];
        long currentMills = getSystemCurrentMills() - 24 * 60 * 60 * 1000 * 3;
        //计算前三天 后三天的dd
        for (int i = 0; i < 7; i++) {
            days[i] = getFormatTime(currentMills, "dd,E");
            currentMills = currentMills + 24 * 60 * 60 * 1000;
            String[] weekday = days[i].split(",");
            Log.v("params", "day==" + days[i] + "  mills=" + currentMills);
            ((TextView) tr_day.getChildAt(i)).setText(weekday[0]);
            ((TextView) tr_week.getChildAt(i)).setText(weekday[1].substring(weekday[1].length() - 1, weekday[1].length()));
            final int finalI = i;
            if (i == 3) {
                setSelect(true, (TextView) tr_day.getChildAt(i));
            }
            (tr_day.getChildAt(i)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int i1 = 0; i1 < tr_day.getChildCount(); i1++) {
                        setSelect(false, ((TextView) tr_day.getChildAt(i1)));
                    }
                    setSelect(true, ((TextView) tr_day.getChildAt(finalI)));
                }
            });
        }
    }

    private void setSelect(boolean b, TextView textView) {
        if (b) {
            textView.setBackgroundResource(R.drawable.ball_red);
            textView.setTextColor(getResources().getColor(R.color.white));
        } else {
            textView.setBackgroundResource(0);
            textView.setTextColor(getResources().getColor(R.color.black));
        }

    }

    private void initViewPager() {

//把Fragment添加到List集合里面
        list = new ArrayList<>();
        list.add(BiFenChildFragment.newInstance(0));
        list.add(BiFenChildFragment.newInstance(1));
        adapter = new TabFragmentPagerAdapter(getChildFragmentManager(), list);
        vp_bifen.setAdapter(adapter);
        vp_bifen.setCurrentItem(0);  //初始化显示第一个页面
        vp_bifen.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                initTag(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //选择赛事监听
        GetMatchNameBack.setGetMatchs(this);
    }

    private void initTag(int index) {
        switch (index) {
            case 0:
                //选中第一个tab
                tabitem_1.setBackgroundDrawable((getResources().getDrawable(R.drawable.tab_left_select_style_bg_white)));
                tabitem_2.setBackgroundDrawable((getResources().getDrawable(R.drawable.tab_right_select_style_bg_red)));
                tabitem_2.setTextColor(getResources().getColor(R.color.white));
                tabitem_1.setTextColor(getResources().getColor(R.color.baseRed));
//                if (fragment1 == null) {
//                    fragment1 = WagesBillsFragment.newInstance("$index", "")
//                    fragmentTransaction !!.add(R.id.frameLayout_wagesbills, fragment1)
//                } else {
//                    fragmentTransaction !!.show(fragment1)
//                }
                break;
            case 1:
                //选中第二个tab
                tabitem_1.setBackgroundDrawable((getResources().getDrawable(R.drawable.tab_left_select_style_bg_red)));
                tabitem_2.setBackgroundDrawable((getResources().getDrawable(R.drawable.tab_right_select_style_bg_white)));
                tabitem_1.setTextColor(getResources().getColor(R.color.white));
                tabitem_2.setTextColor(getResources().getColor(R.color.baseRed));
//                if (fragment2 == null) {
//                    fragment2 = WagesBillsFragment.newInstance("$index", "")
//                    fragmentTransaction !!.add(R.id.frameLayout_wagesbills, fragment2)
//                } else {
//                    fragmentTransaction !!.show(fragment2)
//                }
                break;
        }
        vp_bifen.setCurrentItem(index);
    }


    @OnClick({R.id.tabitem_1, R.id.tabitem_2})
    void changeTab(View view) {
        initTag(Integer.parseInt(view.getTag().toString()));
    }

    @OnClick({R.id.tv_bifen_saishi_choose})


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bifen_saishi_choose:

                if (matchNames!=null){
                    //赛事选择
                    BiFenChooseGameDialog biFenChooseGameDialog = new BiFenChooseGameDialog(getActivity(), true,matchNames);
                    biFenChooseGameDialog.show();
                    biFenChooseGameDialog.setCallBack(new OnResultListener() {

                        @Override
                        public void onReturnResult(String str) {
                            DisPlay(str);
                        }
                    });
                }
                break;
        }
    }


    @Override
    public void getNames(String[] names) {
        if (names!=null||names.length>0){
            matchNames=names;
        }

    }
}
